package com.bsdk.install.device.lockLora.v2.cmd;

import com.alibaba.fastjson2.JSON;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * @projectName: lock
 * @package: com.hlslock.lock.cmd
 * @className: Cmd
 * @author: HansYoung
 * @description: TODO
 * @date: 2023/12/6 14:14
 * @version: 1.0
 */

@NoArgsConstructor
@Getter
public class CMD {
    protected String uId;
    //推送对象
    protected String cMac;
    //标题
    protected String devId;
    //内容
    protected String cmd;
    protected String data;


    public CMD(String uId, String cMac, String devId, String cmd, String data) {
        this.uId = uId;
        this.cMac = cMac;
        this.devId = devId;
        this.cmd = cmd;
        this.data = data;
    }

    public String Packet_data(byte packet_type, byte Cmd, byte[] m_data, int len) {
        int j = 0, i;
        byte xor;
        byte[] buffer = new byte[100];
        StringBuilder Str_buf = new StringBuilder();
        buffer[j++] = 0x02;
        buffer[j++] = (byte) (len + 2);
        buffer[j++] = packet_type;
        buffer[j++] = Cmd;
        for (i = 0; i < len; i++) {
            buffer[j++] = m_data[i];
        }
        buffer[j++] = 0x03;
        xor = check_xor(buffer, j);
        buffer[j++] = xor;
        for (i = 0; i < j; i++) {
            Str_buf.append(String.format("%02X", buffer[i]));
        }
        return Str_buf.toString();

    }

    public StringBuilder buildStartEndTime(Long startTime, Long endTime) {

        StringBuilder sb = new StringBuilder();

        //  date formater
        SimpleDateFormat year = new SimpleDateFormat("yy", Locale.CHINA);
        SimpleDateFormat month = new SimpleDateFormat("MM", Locale.CHINA);
        SimpleDateFormat date = new SimpleDateFormat("dd", Locale.CHINA);
        SimpleDateFormat hour = new SimpleDateFormat("HH", Locale.CHINA);
        SimpleDateFormat minus = new SimpleDateFormat("mm", Locale.CHINA);
        SimpleDateFormat seconds = new SimpleDateFormat("ss", Locale.CHINA);

        // startTime
        sb.append(stringToHex(year.format(new Date(startTime))));
        sb.append(stringToHex(month.format(new Date(startTime))));
        sb.append(stringToHex(date.format(new Date(startTime))));
        sb.append(stringToHex(hour.format(new Date(startTime))));
        sb.append(stringToHex(minus.format(new Date(startTime))));
        sb.append(stringToHex(seconds.format(new Date(startTime))));
        // endTime
        sb.append(stringToHex(year.format(new Date(endTime))));
        sb.append(stringToHex(month.format(new Date(endTime))));
        sb.append(stringToHex(date.format(new Date(endTime))));
        sb.append(stringToHex(hour.format(new Date(endTime))));
        sb.append(stringToHex(minus.format(new Date(endTime))));
        sb.append(stringToHex(seconds.format(new Date(endTime))));

        return sb;
    }

    // 字符串转十六进制
    private final String stringToHex(String s) {
        return String.format("%02X", Integer.valueOf(s));
    }

    private final String stringToHex(Integer s) {
        return String.format("%02X", s);
    }

    // 包验证
    protected final String xor(String s) {
        Integer ch = 0;
        if (s.length() % 2 != 0) throw new IndexOutOfBoundsException("验证包的长度(" + s.length() + ") 不是偶数");
        for (int i = 0; i < s.length(); i++) {
            ch ^= Integer.valueOf(s.substring(i, ++i + 1), 16);
        }

        return Integer.toHexString(ch);
    }

    protected final byte check_xor(byte[] m_data, int len) {
        byte xor = 0;
        int i;
        for (i = 0; i < len; i++) {
            xor ^= m_data[i];
        }
        return xor;

    }

    @Override
    public String toString() {

        return JSON.toJSONString(this);
    }
}